import urllib.request as ur
import json
import pymongo
import time

# 得到mongoDB的连接
conn = pymongo.MongoClient('localhost',27017)
# 选择数据库
db = conn.spider_19_1
# 定义集合名称
zhilian = db.zhilian


keyword = 'python'
for page in range(1,10):
    url = 'https://fe-api.zhaopin.com/c/i/sou?start=%s&pageSize=90&cityId=530&salary=0,0&workExperience=-1&education=-1&companyType=-1&employmentType=-1&jobWelfareTag=-1&kw=python&kt=3&=0' % ((page-1) * 90)
    request = ur.Request(
        url=url,
        headers={
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
            'Cookie':'adfbid=0; adfbid2=0; x-zp-client-id=4b84205c-a006-40a3-96a2-462f455b0247; sts_deviceid=16d243e3232444-04b03554772e83-67e153a-1327104-16d243e323370f; sts_sg=1; sts_chnlsid=121122523; zp_src_url=https%3A%2F%2Fwww.baidu.com%2Fbaidu.php%3Fsc.K00000KbUiqGbb95anQxE2XKcgv1qfW76EtSS_3PsIImMknIWGGAKtPcxRDqJe6VdYm1ScKwedCTETyyE_Z2tAlLUPNwhIXNmC4AyY1Nm8ykCBN61Op-6tye6Z0k4bQt9wlVOfa66R_RSIgZ4Ivjd06ByteYWWmSX-b-XkZDgETf_I4afOTvskhsAglrvwH8mmNI9lfDenhv6H1xL6.DR_NR2Ar5Od669BCXgjRzeASFDZtwhUVHf632MRRt_Q_DNKnLeMX5DkgboozuPvHWdWxfik6zXreke5Zkt8vEWEtX1ugu83vX5b1koLellqLHEvIM-WEl3FJQ7Na9WWOqmc2QnN1tL-sdn8kEBwKnMRSr6hUE6CpXyPvap7Q7erQKSU3X8a9G4I2UM3PQZsOhSZo6CpXy6hUikbvXPXOxYeS8a9G4mgwRDkRA2Ago6CpXy7MHWuxJBmJYgR72s1f_ur5W3qB60.U1Yk0ZDqd_xKJ6Kspynqn0KY5IUZspC0pyYqnWcd0ATqUvNsT1D0Iybqmh7GuZR0TA-b5Hf0mv-b5H6Y0AdY5HDsnHIxnH0krNtknjfYg1nvnjD0pvbqn0KzIjY3PW60uy-b5Hc4PWnYg1DYPHNxnWDknWuxnWbsP1PxnWbdnWPxnWbdnH-xnWbLnWKxnWbdPWIxnWbznW7xnWm1PHKxnWbsP1FxnWbdrjb0mhbqnHR3g1csP0KVm1Ykrjn1PjTvnWnsg1Dsnj7xnH01g100TgKGujYs0Z7Wpyfqn0KzuLw9u1Ys0A7B5HKxn0K-ThTqn0KsTjY1n1f1nHR3nHR0UMus5H08nj0snj0snj00Ugws5H00uAwETjYs0ZFJ5H00uANv5gKW0AuY5H00TA6qn0KET1Ys0AFL5HDs0A4Y5H00TLCq0A71gv-bm1dsTzdMXh410A-bm1dcHbc0IZN15HDkP1DdrjTsrHmYrjbdPWmzn1Rk0ZF-TgfqnHRvrjcLnj64rHbzP0K1pyfquj7-nvDsnHDsnjDvPWwbnsKWTvYqnWKKwjmkPHcLrHKjPjNaw0K9m1Yk0ZwdIjYk0ZK85H00TydY5H00Tyd15H00XMfqn0KVmdqhThqV5HKxn7tknjfvg100uA78IyF-gLK_my4GuZnqn7tsg1D3rjDvrH-xn0Ksmgwxuhk9u1Ys0AwWpyfqn0K-IA-b5iYk0A71TAPW5H00IgKGUhPW5H00Tydh5H00uhPdIjYs0ZGsUZN15H00mywhUA7M5H60UAuW5H00UvnqnfKEIjYs0AqzTZfqnanscznsc10WnansQW0snj0snansczns0APzm1YvnWTsrf%26word%3D%25E6%2599%25BA%25E8%2581%2594%26ck%3D5939.2.72.248.423.272.433.180%26shh%3Dwww.baidu.com%26us%3D1.0.1.0.1.541.0%26bc%3D110101; sajssdk_2015_cross_new_user=1; at=ded97e555f0646b49a954723ce4e00da; rt=391d481277ce47f5b6affceafdf68cb7; ZP_OLD_FLAG=false; jobRiskWarning=true; dywec=95841923; dywez=95841923.1568271155.1.1.dywecsr=landing.zhaopin.com|dyweccn=(referral)|dywecmd=referral|dywectr=undefined|dywecct=/register; __utmc=269921210; __utmz=269921210.1568271155.1.1.utmcsr=landing.zhaopin.com|utmccn=(referral)|utmcmd=referral|utmcct=/register; Hm_lvt_38ba284938d5eddca645bb5e02a02006=1568271155; LastCity%5Fid=763; LastCity=%E5%B9%BF%E5%B7%9E; privacyUpdateVersion=2; CLOSEDIALOG=1568271162830; acw_tc=2760825315682713060045909ecc853032b197d1045d677f0b2f70cf61c397; sou_experiment=unexperiment; POSSPORTLOGIN=4; CANCELALL=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22680534645%22%2C%22%24device_id%22%3A%2216d243e325e68b-08a5b1e9f53541-67e153a-1327104-16d243e325f83f%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_utm_source%22%3A%22baiduPC%22%2C%22%24latest_utm_medium%22%3A%22CPC%22%2C%22%24latest_utm_campaign%22%3A%22hydt%22%2C%22%24latest_utm_content%22%3A%22tj%22%2C%22%24latest_utm_term%22%3A%2228719969%22%7D%2C%22first_id%22%3A%2216d243e325e68b-08a5b1e9f53541-67e153a-1327104-16d243e325f83f%22%7D; urlfrom=121126445; urlfrom2=121126445; adfcid=none; adfcid2=none; __utma=269921210.456649656.1568271155.1568271155.1568274165.2; __utmt=1; __utmb=269921210.1.10.1568274165; dywea=95841923.689452139572700700.1568271155.1568271155.1568274165.2; dyweb=95841923.1.10.1568274165; sts_sid=16d246cae6e1e1-041c0920b651d2-67e153a-1327104-16d246cae6f930; ZL_REPORT_GLOBAL={%22sou%22:{%22actionid%22:%227980fde5-c986-42fe-a740-49cb7d608faa-sou%22%2C%22funczone%22:%22smart_matching%22}}; Hm_lpvt_38ba284938d5eddca645bb5e02a02006=1568274172; sts_evtseq=4',
        }
    )
    response = ur.urlopen(request).read()
    data = json.loads(response)
    print(data['data']['results'])
    time.sleep(1)
    zhilian.insert_many(data['data']['results'])

    break